<?php

/**
 * Description of ProjectFasering
 *
 * @author GHoogendoorn
 * @version 0.1
 * 
 * Version history
 * 0.1  GHoogendoorn    Initial version
 */

class ProjectFasering {
    
    private $fase_ids = array();
    private $fasering = array();
    

    public function __construct() {
        //Initialisatie
    }
    

    public function loadFaseList(){
        $db_fase = new DbFase();
        
        $this->fase_ids = $db_fase->getFaseIds();
  
        foreach( $this->fase_ids as $id ){
            $this->fasering[$id] = new Fase($id);
        }
    }
    
    public function getFirsthFase(){
        
        reset($this->fasering);
        return $this->getNextFase();
    }
    
    public function getNextFase(){
        
        if ( !$this->isLoadedFasering() ){
            
            $this->loadFaseList();
        }
        
        $entry = next($this->fasering);
        
        
        /*
        echo __FILE__.__LINE__.__FUNCTION__.'<br />';
        echo '<pre>';
        var_dump($entry);
        echo '</pre>';
        //*/
        
        if ( !is_object($entry) ){
            /**
             *  If no object entry: Assume end of array.
             */
            reset($this->fasering);
            return FALSE;
        }
        
        
        /*
        echo __FILE__.__LINE__.__FUNCTION__.'<br />';
        echo '<pre>';
        var_dump($entry);
        echo '</pre>';
        //*/
       
        return $entry;
        
    }
    
    
    /**
     * This method returns the next fasestep.
     * 
     * @return array containing the fase steps 
     */
    public function getNextFaseStep(){
        
        
        if ( !$this->isLoadedFasering() ){
            /**
             * Return false, because the function getNextFase should be
             * called firsth: This will load the fases.
             */
            return false;
            
        }
        
        $fase_obj = current( $this->fasering );
        
        
        //*
        echo __FILE__.__LINE__.__FUNCTION__.'<br />';
        echo '<pre>';
        var_dump($fase_obj);
        echo '</pre>';
        //*/
        
        
        $fase_step_array = current( $this->fasering);
        
        if ( !is_array($entry) ){
            /**
             *  If no array entry: Assume end of array.
             */
            reset($this->fasering);
            return FALSE;
        }
       
        return $entry;
        
    }
    
    /**
     *
     * @return int Total number of steps in all fases of current class.
     */
    public function getTotalNrSteps(){
        
        reset($this->fasering);
        
        $steps = 0;
        foreach ( $this->fasering as $idx => $obj ){
            $steps += $obj->getFaseStepCount();
        } 
        
        return $steps;
    }
    
    /**
     *
     * @return int Number of fases in current class.
     */
    public function getFaseNumber(){
        
        if ( !$this->isLoadedFasering() ){
            
            $this->loadFaseList();
        }
        
        return count($this->fasering);
    }
    
    private function isLoadedFasering(){
        
        if ( (!is_array($this->fasering)) || (empty($this->fasering)) ){
            
            return false;
        }
        
        return true;
    }

}

class TestProjectFasering {

    public function TestProjectFasering() {
        try {
            $test = new ProjectFasering();
            $test->loadFaseList();
            
            echo '<hr />';
            echo '/**** aantal fases getFaseCount ***/<br />';
            echo 'Number of fases:'.$test->getFaseNumber(). '<br />';
            echo 'Total number of steps '. $test->getTotalNrSteps() .'<br />';
            echo '<hr />';
            /**
             * Get the firsth fase
             */
            $obj = $test->getFirsthFase();
            
            //echo '<table>';
            do {
                
                /**
                 * For each fase: Get all steps and print.
                 */
                //*
                echo __FILE__.__LINE__.__FUNCTION__.'<br />';
                echo '<pre>';
                var_dump($obj);
                echo '</pre>';
                echo 'Number of steps: '.$obj->getFaseStepCount().'<br /><br />';
                
                echo '/**** Haal fases zolang het kan ****/<br />';
                
                // while( $step_obj = $obj->getNextStep();
                //echo '<tr><td>'.  .'</td></tr>';
            } while( $obj = $test->getNextFase());
            //echo '</table>';
            
            echo ' Create fase (steps) bar<br />';
            
            /**
             * Get the firsth fase
             */
            $obj = $test->getFirsthFase();
            
            echo '<table>';
            do {
                
                /**
                 * For each fase: Get all steps and print.
                 */
                echo 'Number of steps: '.$obj->getFaseStepCount().'<br /><br />';
                
                
                
                // while( $step_obj = $obj->getNextStep();
                //echo '<tr><td>'.  .'</td></tr>';
            } while( $obj = $test->getNextFase());
            //echo '</table>';
            
            /*
            echo '<pre>';
            var_dump($test);
            echo '</pre>';
            //*/
            
            
            
        } catch (Exception $e) {
            echo '<pre>';
            echo $e->getTraceAsString();
            echo '</pre>';
            trigger_error($e->getMessage());
        }
    }

}

?>
